<?php
//module game đã chơi của user
if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class user_game_point_module extends CI_Module {

    var $theme_data = array(
        'layout_main' => 'layout',
        'left' => 'include/leftcp',
    );
    public $mytable = "user_game_point";
    public $primaryKey = "ugame_point_id";
    public $resCode = "USER_GAME_POINT";

	public function init() {
		$this->load->model('muser');
	}
	
	public function active() {
		
	}
	
	public function deactive() {
		
	}
	
	public function admin($config) {
		//$this->view('admin', $config);
	}
	
	public function run($config) {
		$game_id = @$this->msystem->get_global('game_id');
		if(isset($game_id ) && $game_id >0){
			$this->update_game($game_id);
		}
		
	}
	
    function field() {
		$fieldData = array();
		$fieldData['use_id'] = 0;
		$fieldData['game_id'] = 0;
		$fieldData['use_score'] = 0;
		$fieldData['ugame_point_create'] = time();
		return $fieldData;
    }
	
	function update_game($game_id = 0, $user_id = 0)
	{
		$time_limit =time() - (5*60);
		if($user_id  == 0){
			$user_id  = $this->muser->use_id;
		}
		if(!$user_id || $user_id<=0)
			return '';
		$check = $this->get(array('use_id'=>$user_id, 'game_id'=>$game_id ,'ugame_point_create > '=>$time_limit));
		if($check){
			return true;
		}
		else {
			$this->insert(array('use_id'=>$user_id, 'game_id'=>$game_id,'use_score'=>1));
			$this->update_user_point($user_id,1);
		}
	}
	
	function update_user_point($user_id = 0, $point = 0){
		$this->db->where("use_id", $user_id);
        $this->db->set('use_scores', 'use_scores+'.$point, false);
        $this->db->update($this->muser->mytable, array());
	}
	
	function get_scores_by_user_time($user_id = 0, $time_start = 0, $time_end = 0, $type_select = '') {
		switch($type_select){
			case 'sum':
				$this->db->select_sum('use_score','total');
			break;
		}
		$this->db->where("ugame_point_create >=",$time_start);
		$this->db->where("ugame_point_create <=",$time_end);
		return $this->get($user_id,'use_id');
	}
}

?>